package itd.newbee.mall.newbeemall.api;

import io.micrometer.common.util.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import itd.newbee.mall.newbeemall.api.param.MallUserLoginParam;
import itd.newbee.mall.newbeemall.common.Constants;
import itd.newbee.mall.newbeemall.config.annotation.TokenToMallUser;
import itd.newbee.mall.newbeemall.service.NewBeeMallUserService;
import itd.newbee.mall.newbeemall.util.Result;
import itd.newbee.mall.newbeemall.util.ResultGenerator;
import jakarta.annotation.Resource;

import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2025/3/11 09:42
 */
@RestController
@Tag(name="新蜂商城用户操作相关接口", description = "v1")
@RequestMapping("/api/v1")
public class NewBeeMallPersonalAPI {
    @Resource
    private NewBeeMallUserService newBeeMallUserService;

    private static final Logger logger = LoggerFactory.getLogger(NewBeeMallPersonalAPI.class);

    @PostMapping("/user/login")
    @Operation(summary = "用户登录接口", description = "传入用户名和密码,返回token")
    public Result<String> login(@RequestBody @Valid MallUserLoginParam mallUserLoginParam){
        String loginResult = newBeeMallUserService.login(mallUserLoginParam.getLoginName(),
                mallUserLoginParam.getPasswordMd5());
        logger.info("login api,loginName={}, loginResult={}",
                mallUserLoginParam.getLoginName(),loginResult);

        //登陆成功
        if (!StringUtils.isEmpty(loginResult) && loginResult.length() == Constants.TOKEN_LENGTH) {
            Result result = ResultGenerator.genSuccessResult();
            result.setData(loginResult);
            return result;
        }
        return ResultGenerator.genFailResult(loginResult);

    }
    @GetMapping(value = "/test1")
    @Operation(summary = "测试接口1", description = "方法中含有@TokenToMallUser的注解")
    public Result<String> test1(@TokenToMallUser @Valid String user){
        Result result = null;
        if(user == null){
            result = ResultGenerator.genErrorResult(416,"未登录");
            return result;
        }else {
            result = ResultGenerator.genSuccessResult("登录验证通过");
        }
        return result;

    }
    @GetMapping(value = "/test2")
    @Operation(summary = "测试接口2", description = "方法中无@TokenToMallUser的注解")
    public Result<String> test2(){
     Result result = ResultGenerator.genSuccessResult("此接口无需验证,登录通过");
        return result;
    }
}
